# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  include ApplicationHelper
  include WhiteListHelper
  
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_otwdb_session_id'
  
  # Globalize
  before_filter :set_locale
  
  # Will set the cookie 'locale' if (and only if) an explicit parameter 'locale'
  # is passed (and is acceptable)
  # If no cookie exists, we look through the list of desired languages for the
  # first one we can accept.
  def set_locale
    if params[:locale]
      # The user has explicitly requested a locale 
      locale_string = params[:locale]
    elsif session[:locale] && !session[:locale].empty?
      locale_string = session[:locale]
    elsif request.env["HTTP_ACCEPT_LANGUAGE"] && !request.env["HTTP_ACCEPT_LANGUAGE"].empty?
      # Try the browser's locale settings.
      locale_string = request.env["HTTP_ACCEPT_LANGUAGE"][/[^,;]+/]
    else
      # the default locale defined in config/environment.rb is used.
    end
    
    # Now we know what the user wants: do we support it?
    if locale_string
      locale_string.downcase!
      if LOCALES.keys.include?(locale_string)
        user_locale = Locale.new(LOCALES[locale_string])
        cookies[:locale_missing] = "0"
      elsif LOCALES.values.include?(locale_string)
        user_locale = Locale.new(locale_string)
        cookies[:locale_missing] = "0"
      else
        if !cookies[:locale_missing]
          flash.now[:notice] = "We do not currently support your locale."
          cookies[:locale_missing] = "1"
        end
      end
    end
    
    if !user_locale
      user_locale = Locale.new(APPLICATION_DEFAULT_LOCALE)
    end
    Locale.set user_locale

    if Locale.active? && Locale.active.language
      session[:locale] = Locale.active.language.code
    end
  end
  
  after_filter :set_charset
  
  # Will send an appropriate charset to the user's browser
  def set_charset
    content_type = headers["Content-Type"] || "text/html" 
    if /^text\//.match(content_type)
      headers["Content-Type"] = "#{content_type}; charset=utf-8" 
    end
  end
  
  before_filter :check_authorization
  
  # Check for a valid authorization cookie and log the user in if found
  def check_authorization
    authorization_token = cookies[:authorization_token]
    if authorization_token and not logged_in?
      user = User.find_by_authorization_token(authorization_token)
      user.login!(session) if user
    end
  end
  
  # Protect a page from unauthorized access
  # Used in individual controllers as a before_filter, eg:
  # before_filter :protect, :except => [ :login, :register ]
  def protect
    unless logged_in?
      protect_redirect("Please log in first", :controller => "user", :action => "login", :save_page => true)
      return false
    end
  end
  
  # Protect from access except by translators
  def protect_for_translator
    user = get_user
    unless user && (user.is_translator || user.is_volcom)
      protect_redirect("You must be an authorized translator to access this page.")
      return false
    end
  end
  
  # Protect from access except by otw
  def protect_for_otw
    user = get_user
    unless user && (user.is_otw || user.is_volcom)
      protect_redirect("You must be an OTW committee member to access this page.")
      return false
    end
  end
  
  # Protect from access except by volcom
  def protect_for_volcom
    user = get_user
    unless user && user.is_volcom
      protect_redirect("You must be a Volunteers Committee member to access this page.")
      return false
    end
  end
  
  def protect_redirect(message, opts = {})
    args = { :controller => "user", :action => "index", :save_page => false }.merge(opts)
    session[:protected_page] = request.request_uri if args[:save_page]
    flash.now[:notice] = message.t
    redirect_to args
  end
  
  # Get the current user and make available to the application
  # from the application_helper
  before_filter :get_user
  
  
  
  
end
